<?php
/*
 * @Author: lokei
 * @Date: 2022-08-31 15:47:43
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-27 11:36:13
 * @Description: 
 */
namespace App\Http\Controllers\Iot\Device;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Iot\Device\DeviceModel;
use App\Models\Iot\Gateway\GatewayModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class PCController extends Controller {

    public function lock(Request $request) {
        $device = DeviceModel::where('id', '=', $request->input('id'))->first();
        $gateway = GatewayModel::where('id', '=', $device->gateway_id)->first();
        if($device != null && $gateway != null) {
            $fields = [
                'pid' => (config('app.proj_identity') != null && config('app.proj_identity') != '') ? config('app.proj_identity') : '',
                'gateway_identity' => $gateway != null ? $gateway->identity : '',
                'topic' => 'pc_lock',
                'device_identity' => $device->identity
            ];
            Redis::xAdd('iot_mqtt_ops', '*', $fields);
            return ResultTool::success();
        }
    }

    public function unlock(Request $request) {
        $device = DeviceModel::where('id', '=', $request->input('id'))->first();
        $gateway = GatewayModel::where('id', '=', $device->gateway_id)->first();
        if($device != null && $gateway != null) {
            $lock_hour = $request->input('hour_duration') ? intval($request->input('hour_duration')) : 0;
            $fields = [
                'pid' => (config('app.proj_identity') != null && config('app.proj_identity') != '') ? config('app.proj_identity') : '',
                'gateway_identity' => $gateway != null ? $gateway->identity : '',
                'topic' => 'pc_unlock',
                'device_identity' => $device->identity,
                'lock_hour' => $lock_hour
            ];
            Redis::xAdd('iot_mqtt_ops', '*', $fields);
            return ResultTool::success();
        }
    }

}